package com.learn.activiti.listener;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Service;

/**
 * 这里用于对每次任务执行完成之后的结果进行统计
 *
 * @author xianglujun
 * @date 2019/7/12 9:50
 */
@Service
@Slf4j
public class IdeaTaskListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("ideaTaskListener start...");
        // 获取当前task的审核结果
        String result = (String) delegateTask.getVariable("auditResult");

        log.info("{} audit result is {}", delegateTask.getAssignee(), result);

        // 这里获取executionListener中设置的reject和agree数量的统计
        int rejectCount = (int) delegateTask.getVariable("rejectCount");
        int agreeCount = (int) delegateTask.getVariable("agreeCount");
        if ("reject".equals(result)) {
            delegateTask.setVariable("rejectCount", ++rejectCount);
        } else if ("agree".equals(result)) {
            delegateTask.setVariable("agreeCount", ++agreeCount);
        }

        log.info("ideaTaskListener end...");
    }
}
